/*
 * Copyright (C) 2009 University of Washington
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 */

package org.odk.collect.android.activities;

import java.util.HashMap;
import java.util.Map;

import org.odk.collect.android.R;
import org.odk.collect.android.provider.InstanceProviderAPI;
import org.odk.collect.android.provider.InstanceProviderAPI.Status;

import android.app.TabActivity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TabHost;
import android.widget.TabWidget;
import android.widget.TextView;

/**
 * A host activity for {@link InstanceChooserList}.
 * 
 * @author Yaw Anokwa (yanokwa@gmail.com)
 */
public class InstanceChooserTabs extends TabActivity implements CountListener {

    // count for tab menu bar
    private static final String SAVED_TAB = "saved_tab";
    private static final String COMPLETED_TAB = "completed_tab";
    private static final String SUBMITTED_TAB = "submitted_tab";
    private static final int FONT_SIZE = 21;
    
    private Map<String, TextView> mTabTitleView = new HashMap<String, TextView>();
    private Map<String, Long> mTabInstanceCount = new HashMap<String, Long>();
    
    private void addTab(String tag, int titleId, String statusString)
    {
        Intent intent = new Intent(this, InstanceChooserList.class);
        intent.putExtra("instance-state", statusString);

        String title = getString(titleId, 0);
        intent.putExtra(CountListener.INTENT_EXTRA_CALLER_ID, titleId + "");
        
        final TabHost tabHost = getTabHost();
        tabHost.addTab(tabHost.newTabSpec(tag)
                .setIndicator(title).setContent(intent));
        tabHost.setCurrentTabByTag(tag);
        // mTabInstanceCount should be populated now
        
        // hack to set font size and padding in tab headers
        // arrived at these paths by using hierarchy viewer
        LinearLayout ll = (LinearLayout) tabHost.getChildAt(0);
        TabWidget tw = (TabWidget) ll.getChildAt(0);

        RelativeLayout rls = 
        		(RelativeLayout) tw.getChildAt(tw.getChildCount() - 1);
        TextView tvs = (TextView) rls.getChildAt(1);
        tvs.setTextSize(FONT_SIZE);
        tvs.setPadding(0, 0, 0, 6);
        
        title = getString(titleId, mTabInstanceCount.get(titleId + ""));
        tvs.setText(title);
        mTabTitleView.put(titleId + "", tvs);
    }
    
    @Override
    public void OnCounted(String callerId, long count) {
    	mTabInstanceCount.put(callerId, count);
    	/*
    	if (mTabTitleView.containsKey(callerId))
    	{
            String title = getString(Integer.parseInt(callerId),
            		mTabInstanceCount.get(callerId));
    		mTabTitleView.get(callerId).setText(title);
    	}
    	*/
    }
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTitle(getString(R.string.app_name) + " > " + getString(R.string.review_data));

        // create tab host and tweak color
        final TabHost tabHost = getTabHost();
        tabHost.setBackgroundColor(Color.WHITE);
        // tabHost.getTabWidget().setBackgroundColor(Color.BLACK);

        // create intent for saved tab
        addTab(SAVED_TAB, R.string.saved_data, Status.incomplete.name());

        // create intent for completed tab
        addTab(COMPLETED_TAB, R.string.completed_data, 
        		InstanceProviderAPI.UNSUBMITTED);

        // create intent for submitted tab
        addTab(SUBMITTED_TAB, R.string.submitted_data, Status.submitted.name());

        getTabHost().setCurrentTabByTag(SAVED_TAB);
    }
}
